package utils;

import java.util.Properties;
import java.util.UUID;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.cinemtl.beans.Movie;
import com.cinemtl.beans.Order;
import com.cinemtl.manager.MovieManager;
import com.cinemtl.manager.UserManager;


public class MailManager {
	// parametre connexion serveur smtp
	private static String user = "etudiant.isi.java2@gmail.com";
	private static String pwd = "abc123...";

	// qui envoie
	private static String from = "etudiant.isi.java2@gmail.com";


	

	
	public static void sendEmail(String msg, String to, String sujet){
		Properties props = new Properties();
		//le smtp de votre serveur web
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");

		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						// Adresse mail et password de l emetteur sur le serveur
						// smtp
						return new PasswordAuthentication(user, pwd);
					}
				});

		try {
			Message message = new MimeMessage(session);
			// adresse mail de l emetteur
			message.setFrom(new InternetAddress(from));
			// adresse destinataire
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
			message.setSubject(sujet);
			message.setContent(msg, "text/html; charset=utf-8");

			Transport.send(message);

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}	
	}
	public static void sendWelcomeMail(String lname, String email){
		String to = email;
		
		String sujet ="Welcome To Cine-MTL!" ;
		
		String messageToSend =  ""
				+ "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r\n"
				+ "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n"
				+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n"
				+ "<head>\r\n"
				+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\r\n"
				+ "<title>Welcome To Cine-MTL</title>\r\n"
				+ "</head>\r\n"
				+ "<body>\r\n"
				+"<h1>Welcome to Cine-MTL</h1>"
				+ "<p>Dear M."+lname+",</p>\r\n"
				+"<p>Your account has been successfully registered. </p>\r\n"
				+ "</body>\r\n" + "</html>";	
		
		sendEmail(messageToSend, to, sujet);
	}
	public static void resetPasswordMail(String email){
		String to = email;
		UUID uuid = UUID.randomUUID();
		String sujet ="Reset Password" ;
		
		String messageToSend =  ""
				+ "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r\n"
				+ "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n"
				+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n"
				+ "<head>\r\n"
				+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\r\n"
				+ "<title>Welcome To Cine-MTL</title>\r\n"
				+ "</head>\r\n"
				+ "<body>\r\n"
				+"<h1>Cine-MTL</h1>"
				+ "<p>Dear User </p>\r\n"
				+"<p>If You requested a password reset please click the following</p>\r\n"
				+"<a href=\"http://localhost:8080/cine-mtl/requestReset?uuid="+uuid.toString()+"\">Reset Password</a>"
				+ "</body>\r\n" + "</html>";	
		UserManager.insertUUID(uuid.toString(), email);
		sendEmail(messageToSend, to, sujet);
	}
	public static void sendOrder(String lname, Order order,String email){
		String to = email;
		Movie movie = MovieManager.getById(order.getIdMovie());
		String sujet ="Order Confirmation from CINE-MTL";
		
		String messageToSend =  ""
				+ "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r\n"
				+ "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n"
				+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n"
				+ "<head>\r\n"
				+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\r\n"
				+ "<title>Order Confirmation</title>\r\n"
				+ "</head>\r\n"
				+ "<body>\r\n"
				+ "<p>Dear M."+lname+",</p>\r\n"
				+"<p>You order has been successfully processed!</p>\r\n"
				+"<h3>Order</h3>\r\n"
				+"<span><b>Movie : </b>"+movie.getTitle()+"</span>\r\n"
				+"<span><b>Ticket(s) : </b>"+order.getNbTicket()+"</span>\r\n"
				+ "</body>\r\n" + "</html>";	
		
		sendEmail(messageToSend, to, sujet);
	}
}
